package com.lagou.edu.controller;

import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */


    @Autowired
    private AccountService accountService;

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<Account>  queryAll() throws Exception {
        return accountService.queryAccountList();
    }


    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(@RequestParam("name") String name, @RequestParam("password") String password, HttpServletRequest request) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        if (accountService.login(name,password,request)) {

            modelAndView.addObject("tlist", accountService.queryAccountList());
            modelAndView.addObject("name", "testname");
            modelAndView.setViewName("success");

            return modelAndView;
        }
        modelAndView.setViewName("error");

        return modelAndView;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public ModelAndView delete(@RequestParam("id") Long id  ) throws Exception {
        System.out.println(id );
        accountService.delteById(id);
      ModelAndView modelAndView = new ModelAndView();
     modelAndView.addObject("tlist",accountService.queryAccountList());
        modelAndView.addObject("name","testname");
        modelAndView.setViewName("success");

        return modelAndView;
    }
}
